%define _duplicate_files_terminate_build 0
# avoid OrderWithRequires from itself
%global __orderwithrequires_exclude basesystem

%define productURL 'http://os.mos.ru'
%define rosa_platform 2021.1
%define rosa_platform_integer 202110
%define rosa_release_mos 12

Summary(ru_RU.UTF-8):	Конфигурационные файлы для брендинга
Summary:	Configs to change branding in one place
Name:		branding-configs
Version:	%{rosa_platform}
Release:	13
Epoch:		2
License:	GPL
Group:		System/Base
Url:		https://hub.mos.ru/mos/pkgs/branding-configs-mos
Vendor:		MOS

Source0: branding-configs.rpmlintrc

# abf fetch
# c=1; find . -maxdepth 1 -type f | grep -vE '.abf.yml$|.spec$|BUILD' | sed -e 's,./,,' | sort -u | while read -r line ; do echo Source${c}: $line ; c=$((++c)); done
Source1:	first_startup.tpl.html
Source2:	fon.png
Source3:	index_en.tpl.html
Source4:	index_offline_en.tpl.html
Source5:	index_offline_ru.tpl.html
Source6:	index_ru.tpl.html
Source7:	index.tpl.html
Source8:	license_ru_mos.txt
Source12:	macros.platform
Source13:	macros.product
Source14:	release-notes-en.tpl.txt
Source15:	logo.svg
Source101:	CREDITS

BuildRequires:	lynx
BuildRequires:	locales-ru

%description
Quick and easy way to change distribution branding in one place.

%description -l ru_RU.UTF-8
Быстрый и простой способ изменения брендинга дистрибутива в одном месте.

#------------------------------------------------------------------

%package common
Summary(ru_RU.UTF-8):	Основные файлы брендинга
Summary:	Branding common files
Group:		System/Base
# cf mdvbz#32631
Provides:	arch(%{_target_cpu}) = %{EVRD}
Provides:	mandriva-release-common = %{EVRD}
Provides:	rosa-release-common = %{EVRD}
Provides:	mos-release-common = %{EVRD}
Provides:	distro-release-common = %{EVRD}
Provides:	distro-release-common%{_isa} = %{EVRD}
Conflicts:	distro-release-common < 2021.1-4
Obsoletes:	distro-release-common < 2021.1-4
Conflicts:	distro-release-rosa%{version} < 2021.1-4
Obsoletes:	distro-release-rosa%{version} < 2021.1-4

%description common
Common files for branding packages.

%description -l ru_RU.UTF-8 common
Пакет с основными файлами брендинга.

%files common
%dir %{_datadir}/doc/HTML
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}/images
%{_datadir}/%{name}/images/*.{png,svg}
%{_datadir}/%{name}/CREDITS
%dir %{_defaultlicensedir}/rosa-distro
%dir %{_sysconfdir}/branding-configs
# static symlinks to dynamic /etc/release
%{_sysconfdir}/rosa-release
%{_sysconfdir}/mandriva-release
%{_sysconfdir}/redhat-release
%{_sysconfdir}/distro-release
%{_sysconfdir}/system-release

#------------------------------------------------------------------

%package rpm-macros
Summary(ru_RU.UTF-8):	Макросы RPM для платформы
Summary:	Platform-wide common RPM macros
Group:		System/Base
# Do not require "rpm" to avoid odd dependencies in super-minimal containers

%description rpm-macros
Platform-wide common RPM macros.

%description rpm-macros
Макросы RPM для платформы.

%files rpm-macros
%{_rpmmacrodir}/macros.platform

#------------------------------------------------------------------

# -b: branch (fresh, chrome, nickel)
# -t: type (desktop, server, rootfs)
# -d: make this flavour be default ("Suggests")
# RPM macros are in a separate package, but /etc/issue, /etc/os-release
# are needed in all minimal installations, including the ones used for
# building packages. We make this provide here and
# "Suggests: branding-configs-default" in basesystem-minimal and rosa-repos-pkgprefs.
# https://fedoraproject.org/wiki/Packaging:WeakDependencies
# This allows to install any branding flavour, but default to a specific one if none are specified.
# -o: product name (e.g.: ROSA fresh Desktop R12), use with %%{quote:...}
# -f: obsolete and replace distro-release-* flavours (abf.io/import/distro-release-{red,res})
# -h: obsolete and replace branding-configs flavours
%define mk_flavour_subpackage(b:dt:o:f:h:) \
\
%{expand: \
# e.g.: branding-configs-fresh-desktop \
%package %{-b*}-%{-t*} \
Summary(ru_RU.UTF-8):	Файлы описания релиза дистрибутива для "%{-o*}" \
Summary:	ROSA distribution release files for "%{-o*}" \
Group:		System/Base \
# /bin/ln \
Requires(posttrans): coreutils \
# Do NOT require rosa-kernel-tools to avoid odd deps in minimal systems, \
# if rosa-kernel-tools is not installed, directory /var/spool/initramfs-regen \
# will not be owned by any package, that is not a big deal I think. \
#Requires:	rosa-kernel-tools \
# No %%EVRD here to easify making customized distros \
Requires: %{name}-common \
# Ensure that needed directories exist \
Requires(posttrans): %{name}-common \
Requires: %{name}-rpm-macros \
# Ensure that %%_rpmmacrodir exists \
Requires(posttrans): %{name}-rpm-macros \
# allow boolean deps like: "Recommends: (foo if distro-type(desktop)" \
Provides: distro-type(%{-t*}) \
# make flavours conflict with each other \
Provides: branding-configs-flavour = %{-b*}_%{-t*} \
Conflicts: branding-configs-flavour < %{-b*}_%{-t*} \
Conflicts: branding-configs-flavour > %{-b*}_%{-t*} \
# \
Provides: %{name} = %{EVRD} \
Provides: %{name}-%{-b*} = %{EVRD} \
Provides: indexhtml = %{EVRD} \
Provides: rosa-release = %{EVRD} \
Provides: mos-release = %{EVRD} \
Provides: redhat-release = %{EVRD} \
Provides: mandriva-release = %{EVRD} \
Provides: distro-release = %{EVRD} \
Provides: system-release = %{EVRD} \
Provides: system-release(%{version}) \
Provides: system-release(releasever) = %{version} \
Provides: distro-release = %{EVRD} \
%{-f:Provides: distro-release-%{-f*}-rosa%{version} = %{EVRD}} \
%{-f:Conflicts: distro-release-%{-f*}-rosa%{version} < 2021.1-4} \
%{-f:Obsoletes: distro-release-%{-f*}-rosa%{version} < 2021.1-4} \
%{-h:Provides: branding-configs-%{-h*} = %{EVRD}} \
%{-h:Conflicts: branding-configs-%{-h*} < 2021.1-4} \
%{-h:Obsoletes: branding-configs-%{-h*} < 2021.1-4} \
%{-d:Provides: branding-configs-default = %{EVRD}} \
\
%description %{-b*}-%{-t*} \
ROSA distribution release files for "%{-o*}". \
\
%description -l ru_RU.UTF-8 %{-b*}-%{-t*} \
Системные файлы, описывающие релиз дистрибутива для "%{-o*}". \
\
%files %{-b*}-%{-t*} \
\
%dir %{_datadir}/%{name}/%{-b*}-%{-t*} \
%{_datadir}/%{name}/%{-b*}-%{-t*}/license_ru.txt \
%{_datadir}/%{name}/%{-b*}-%{-t*}/first_startup.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/index.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/index_ru.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/index_en.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/index_offline_ru.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/index_offline_en.html \
%{_datadir}/%{name}/%{-b*}-%{-t*}/macros.product \
%{_sysconfdir}/branding-configs/os-release.%{-b*}-%{-t*} \
%{_sysconfdir}/branding-configs/release.%{-b*}-%{-t*} \
%{_sysconfdir}/branding-configs/lsb-release.%{-b*}-%{-t*} \
/var/spool/initramfs-regen/branding-configs-%{-b*}-%{-t*} \
%ghost %{_sysconfdir}/lsb-release \
%ghost %{_sysconfdir}/os-release \
%ghost %{_sysconfdir}/release \
%ghost %{_sysconfdir}/rosa-release \
%ghost %{_sysconfdir}/mos-release \
%ghost %{_datadir}/doc/HTML/index.html \
%ghost %{_datadir}/doc/HTML/first_startup.html \
%ghost %{_defaultlicensedir}/rosa-distro/license_ru.txt \
%ghost %{_sysconfdir}/issue \
%ghost %{_sysconfdir}/issue.net \
\
# Using %%posttrans, not %%post, to simplify dependency graph \
# and avoid cycled deps; I believe that scriptlets of packages \
# do not rely on existense of files like /etc/os-release etc., \
# at least I do not see any good reasons for them to require its existense \
# during bootstrap of chroot/ISO image. \
%posttrans %{-b*}-%{-t*} \
cd /etc \
ln -sf branding-configs/os-release.%{-b*}-%{-t*} os-release \
ln -sf branding-configs/release.%{-b*}-%{-t*} release \
ln -sf branding-configs/lsb-release.%{-b*}-%{-t*} lsb-release \
# /usr/share/licenses/rosa-distro \
cd %{_defaultlicensedir}/rosa-distro \
ln -sf ../../branding-configs/%{-b*}-%{-t*}/license_ru.txt license_ru.txt \
# /usr/share/doc \
cd %{_datadir}/doc/HTML \
ln -sf ../../branding-configs/%{-b*}-%{-t*}/first_startup.html first_startup.html \
ln -sf ../../branding-configs/%{-b*}-%{-t*}/index.html index.html \
# /usr/lib/rpm/macros.d
# Note that this does not make requirement from "rpm" package
# because installation of branding-configs-rpm-macros has already created this directory \
# even if rpm itself is not installed (useful for super-minimal container images) \
cd %{_rpmmacrodir} \
ln -sf ../../../share/branding-configs/%{-b*}-%{-t*}/macros.product macros.product \
}

#------------------------------------------------------------------
%mk_flavour_subpackage -b mos -t desktop -o %{quote:MOS Desktop %{rosa_release_mos}}
%mk_flavour_subpackage -b mos -t panel -o %{quote:MOS Panel %{rosa_release_mos}}
%mk_flavour_subpackage -b mos -t rootfs -o %{quote:MOS rootfs %{rosa_release_mos}}
%mk_flavour_subpackage -b mos -t server -o %{quote:MOS Server %{rosa_release_mos}}
#------------------------------------------------------------------


%prep

%build
cp %{sources} .

%install
set -u

# Ensure that relatives symlinks in %%postrrans are correct
[ "%{_defaultlicensedir}" = "/usr/share/licenses" ]
[ "%{_rpmmacrodir}" = "/usr/lib/rpm/macros.d" ]
mkdir -p %{buildroot}%{_defaultlicensedir}/rosa-distro
mkdir -p %{buildroot}/etc/branding-configs

# avoid message "Creating dir /builddir/tmp" from lynx into stdout
export TMPDIR="$PWD/tmp"
mkdir "$TMPDIR"

_mk_flavour(){

# fresh, chrome, nickel
local branch="$1"
# desktop, server, rootfs
local type="$2"
# e.g. R12 or X5
local os_version="$3"
# e.g. "ROSA Fresh Desktop"
local os_name="$4 ${os_version}"

mkdir -p %{buildroot}%{_datadir}/branding-configs/${branch}-${type}
# %{_defaultlicensedir}/rosa-distro/license_ru.txt will be symlinked to it
install -m644 %{SOURCE8} %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/license_ru.txt
# %{_datadir}/doc/HTML/index.html will be symlinked to it
sed index.tpl.html -e "s,@configs_subdir@,${branch}-${type},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/index.html
# %{_datadir}/doc/HTML/first_startup.html will be symlinked to it
sed first_startup.tpl.html -e "s,@OS_NAME@,${os_name},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/first_startup.html
# it won't be symlinked to
sed index_ru.tpl.html -e "s,@OS_NAME@,${os_name},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/index_ru.html
sed index_offline_ru.tpl.html -e "s,@OS_NAME@,${os_name},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/index_offline_ru.html
# it won't be symlinked to
sed index_en.tpl.html -e "s,@OS_NAME@,${os_name},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/index_en.html
sed index_offline_en.tpl.html -e "s,@OS_NAME@,${os_name},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/index_offline_en.html
# /usr/lib/rpm/macros.d/macros.product will be symlinked to it
sed macros.product -e "s,@OS_BRANCH@,${branch},g" -e "s,@OS_TYPE@,${type},g" -e "s,@OS_VERSION@,${os_version},g" > %{buildroot}%{_datadir}/branding-configs/${branch}-${type}/macros.product

# /etc/os-release will be symlinked to it
# Specification: https://www.freedesktop.org/software/systemd/man/os-release.html
# Avoiding symlinks from /etc/ to /usr because they may theoretically be different partitions on disk,
# so putting it into /etc/branding-release instead of %{_datadir}/%{name}/${branch}-${type}
# ! "A relative symlink instead of an absolute symlink is necessary
#    to avoid breaking the link in a chroot or initrd environment such as dracut"
cat > %{buildroot}/etc/branding-configs/os-release.${branch}-${type} << EOF
# Standard values
# Specification: https://www.freedesktop.org/software/systemd/man/os-release.html
NAME="${4}"
ID=mos
ID_LIKE=rosa
PRETTY_NAME="${os_name}"
CPE_NAME="cpe:/o:mos:rosa:%{rosa_platform}"
META_CLASS=${type}
VARIANT_ID=${branch}-${type}
ANSI_COLOR="1;43"
VERSION="${os_version}"
VERSION_ID=%{rosa_platform}
VERSION_CODENAME=${os_version}
HOME_URL="http://os.mos.ru"
LOGO=mos
BUG_REPORT_URL="https://hub.mos.ru/mos"
# ROSA-specific values
ROSA_OS_BRANCH=${branch}
ROSA_OS_PLATFORM="rosa%{rosa_platform}"
ROSA_OS_PLATFORM_INTEGER=%{rosa_platform_integer}
ROSA_OS_TYPE=${type}
ROSA_OS_VERSION=${os_version}
EOF

# /etc/release will be symlinked to here
cat > %{buildroot}/etc/branding-configs/release.${branch}-${type} << EOF
${os_name} release %{rosa_platform} for %{_target_cpu}
EOF

# Actualy lsb-release file is probably not needed, most distros don't have it
# /etc/lsb-release will be symlinked to here
cat > %{buildroot}/etc/branding-configs/lsb-release.${branch}-${type} << EOF
LSB_VERSION=%{rosa_platform}
DISTRIB_ID=rosa
DISTRIB_RELEASE=${os_version}
DISTRIB_CODENAME=${os_version}
DISTRIB_DESCRIPTION="${os_name}, platform %{rosa_platform}"
EOF

# /etc/product.id existed and is specified here: https://wiki.mageia.org/en/Product_id
# but it is not used nowadays, so not creating it. Instead some potentially useful
# $ROSA_* variables are added to /etc/os-release above.

# Trigger rebuidling initrd if rosa-kernel-tools is installed
# (and it is installed everywhere where initrd exists).
# Initrd contains /etc/os-release etc.
mkdir -p %{buildroot}/var/spool/initramfs-regen
touch %{buildroot}/var/spool/initramfs-regen/branding-configs-${branch}-${type}

}

_mk_flavour mos desktop %{rosa_release_mos} "MOS Desktop"
_mk_flavour mos panel %{rosa_release_mos} "MOS Panel"
_mk_flavour mos rootfs %{rosa_release_mos} "MOS rootfs"
_mk_flavour mos server %{rosa_release_mos} "MOS Server"

# Static relative symlinks to dynamic /etc/os-release
( cd %{buildroot}/etc
  for i in rosa mandriva redhat distro system mos
  do
    ln -s release "$i"-release
  done
)

%install_macro platform macros.platform

mkdir -p %{buildroot}%{_datadir}/doc/HTML

# subdirectory "images"
mkdir -p %{buildroot}%{_datadir}/branding-configs/images
cp %{SOURCE2} %{SOURCE15} %{buildroot}%{_datadir}/branding-configs/images

install -m0644 %{SOURCE101} %{buildroot}%{_datadir}/%{name}/CREDITS

set +u
